package site.immonkey.ftptest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

import javax.net.ssl.SSLSocket;

import site.immonkey.ftptest.utils.TLSUtils;

public class FtpsUtil extends FtpUtil {
	public final static int DEFAULT_PORT = 990;

	private SSLSocket sslConnection;

	public FtpsUtil(String host) throws IOException {
		super(host, DEFAULT_PORT);
	}

	public FtpsUtil(String host, int port) throws IOException {
		super(host, port);
	}

	@Override
	protected void connect() throws IOException {
		connection = new Socket(host, port);

		sslConnection = TLSUtils.ssl(connection);
		_reader = new BufferedReader(new InputStreamReader(sslConnection.getInputStream(), encode));
		_writer = new BufferedWriter(new OutputStreamWriter(sslConnection.getOutputStream(), encode));
		readResponse();
		sendComand("PROT P");
	}

	@Override
	public Socket pasv() throws IOException {
		Socket pasvSocket = super.pasv();
		return TLSUtils.ssl(pasvSocket);
	}

}
